package utils

import (
	"github.com/bwmarrin/snowflake"
	"sync"
	"time"
)

// 使用snowflake雪花算法生成id
var (
	node *snowflake.Node
	once sync.Once
)

// 初始化雪花算法节点
func InitSnowflake(nodeID int64) error {
	var err error
	once.Do(func() {
		snowflake.Epoch = time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano() / 1e6
		node, err = snowflake.NewNode(nodeID)
	})
	return err
}

// 生成唯一的ID
func GenerateID() int64 {
	if node == nil {
		panic("snowflake node not initialized")
	}
	return node.Generate().Int64()
}
